package com.qmds.userserver.user.sms;

import com.aliyun.dysmsapi20170525.*;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.dysmsapi20170525.models.SendSmsResponse;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;

/**
 * 指尖造梦，精益求精
 *
 * @Author: Feri
 * @Date:2024/8/12 15:25
 * @Modified:
 */
public class AliSmsUtil {
    public static final String TEM_RCODE= "SMS_473000054";
    public static final String KEY= "LTAI5tKj8JkoX7gYZZQGAteX";
    public static final String SECRET= "Oh4jtLLUXokAwARAONHSZMJcmONsER";

    public static Client client;
    static {
        Config config = new Config()
                .setAccessKeyId(KEY)
                .setAccessKeySecret(SECRET);
        config.endpoint = "dysmsapi.aliyuncs.com";
        try {
            client= new Client(config);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }


    /**
     * 发送短信验证码
     * @param tem 短信模板
     * @param phone 手机号
     * @param code 验证码*/
    public static boolean sendCode(String tem,String phone,String code){
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(phone)
                .setSignName("千锋油卡")//
                .setTemplateCode(tem)
                .setTemplateParam("{\"code\":\""+code+"\"}");

        RuntimeOptions runtime = new RuntimeOptions();
        try {
            // 复制代码运行请自行打印 API 的返回值
            SendSmsResponse response=client.sendSmsWithOptions(sendSmsRequest, runtime);
            return "OK".equals(response.getBody().code);
        } catch (Exception error) {
            error.printStackTrace();
        }
        return false;
    }
}
